package com.fyzk.activity.user.accountdetail;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.widget.AbsListView;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.fyzk.activity.lottery.togetherbuy.HM_VerticalPop;
import com.fyzk.activity.user.RecordBaseActivity;
import com.fyzk.activity.user.ZhangHuMXRecordView;
import com.fyzk.adapter.ZhangHuMXPagerAdP;
import com.fyzk.bean.AccountDetailResponse;
import com.fyzk.bean.AccountDetailsBean;
import com.fyzk.data.preference.MySharedPreferences;
import com.fyzk.net.Commands;
import com.fyzk.view.TopTitleView;
import com.tyjc.lottery.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import http.Common.CommandUtil;
import http.StringResultBean;

/**
 * 帐户明细
 */
public class AccountStatementActivity extends RecordBaseActivity implements HM_VerticalPop
        .HM_PopuponItemListener {

    protected TopTitleView zhhmx_title_rl;

    private String[] popup_items = { "全部","近一周","近一个月", "近三个月"};
    private int[] range_time = {1,2,3,4};
    private HM_VerticalPop top_popup;

    //	private TextView tv_all, tv_chongzhi, tv_touzhu, tv_paijiang, tv_tixian;
    private RadioGroup zhmx_filter;


    private int search_all = 1;
    private int search_bet = 1;
    private int search_cz = 1;
    private int search_tx = 1;
    private int search_prize = 1;

    protected int request_type = -1;

    private ViewPager bottom_vp;
    private ZhangHuMXPagerAdP bottom_vp_adp;
    private List<ZhangHuMXRecordView> vp_views = new ArrayList<ZhangHuMXRecordView>();

    /**
     * 当前选项卡位置
     */
    private int curPage = 0;
    private static final int XXK_ALL = 0;
    private static final int XXK_CHONGZHI = 1;
    private static final int XXK_BET = 2;
    private static final int XXK_PRIZE = 3;
    protected static final int XXK_TIXIAN = 4;

    public static final int TYPE_ALL = -1;
    public static final int TYPE_BET = 1;
    public static final int TYPE_CZ = 2;
    public static final int TYPE_TX = 5;
    public static final int TYPE_PRIZE = 4;

    protected int timeRange = 2;

    private TextView balance;
    private TextView withdraw;
    private TextView freeze;
    private RelativeLayout rlBg;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zhanghumx);
        info();

        zhhmx_title_rl.setRightTxt("近一周");
        zhhmx_title_rl.getRightTxt().setTextColor(getResources().getColor(R.color.top_title));
        zhhmx_title_rl.getRightTxt().setCompoundDrawablesWithIntrinsicBounds(null,null,getResources().getDrawable(R.drawable.icon_arrow_down,null),null);
        net();
    }

    protected void net() {
        if (this.search == 1 && !isUpdate) {
            vp_views.get(curPage).showLoading();
        }
        if(curPage == XXK_TIXIAN){
            request_withdrawrecord();
        }else{
            request_accountDetail();
        }
    }

    protected void request_accountDetail() {
        JSONObject request_json = new JSONObject();
        try {
            request_json.put("payType", request_type);
            request_json.put("pageIndex", search);
            request_json.put("maxResult", 20);
            request_json.put("timeRange", timeRange);
            httpPostJson(CommandUtil.ACCOUNT_LIST, Commands.FINDUSERACCOUNTDETAILS, request_json,
					false);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }



    /**
     * pageIndex	int	页码
     * maxResult	int	每页记录数
     * timeRange	int	时间范围
     */
    protected void request_withdrawrecord() {
        JSONObject request_json = new JSONObject();
        try {
            request_json.put("pageIndex", search);
            request_json.put("maxResult", 20);
            request_json.put("timeRange", timeRange);
            httpPostJson(CommandUtil.FINDUSERDRAWAMOUNTS, Commands.FINDUSERDRAWAMOUNTS,
                    request_json, false);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }




    private AccountDetailResponse accountDetailResponse;

    @Override
    protected void onSuccess(int requestCode, StringResultBean resultBean, String result) {
        super.onSuccess(requestCode, resultBean, result);

        switch (requestCode){
            case CommandUtil.ACCOUNT_LIST:
                accountDetailResponse = JSON.parseObject(resultBean.getData(), AccountDetailResponse.class);
                List<AccountDetailsBean> infos = accountDetailResponse.getDetails();
                int payId;
                try {
                    payId = request_type;
                } catch (Exception e) {
                    payId = 0;
                }
                if(search == 1 && infos.size() == 0){
                    vp_views.get(curPage).showNoneRecord(resultBean.getMessage());
                }else{
                    vp_views.get(curPage).showData();
                }
                switch (payId) {
                    case TYPE_ALL:
                        if (search == 1) {
                            vp_views.get(XXK_ALL).initRecordList(infos);
                        } else {
                            vp_views.get(XXK_ALL).addRecordInfos(infos);
                        }
                        break;
                    case TYPE_CZ:
                        if (search == 1) {
                            vp_views.get(XXK_CHONGZHI).initRecordList(infos);
                        } else {
                            vp_views.get(XXK_CHONGZHI).addRecordInfos(infos);
                        }
                        break;
                    case TYPE_BET:

                        if (search == 1) {
                            vp_views.get(XXK_BET).initRecordList(infos);
                        } else {
                            vp_views.get(XXK_BET).addRecordInfos(infos);
                        }
                        break;
                    case TYPE_PRIZE:
                        if (search == 1) {
                            vp_views.get(XXK_PRIZE).initRecordList(infos);
                        } else {
                            vp_views.get(XXK_PRIZE).addRecordInfos(infos);
                        }
                        break;
                    case TYPE_TX:
                        if (search == 1) {
                            vp_views.get(XXK_TIXIAN).initRecordList(infos);
                        } else {
                            vp_views.get(XXK_TIXIAN).addRecordInfos(infos);
                        }
                        break;
                    default:
                        break;
                }

                break;
            case CommandUtil.FINDUSERDRAWAMOUNTS:
                try {
                    JSONObject jsonObject = new JSONObject(resultBean.getData());
                    String details = jsonObject.getString("details");
                    List<AccountDetailsBean> withdrawInfos = JSON.parseArray(details, AccountDetailsBean.class);
                    if (search == 1) {
                        vp_views.get(XXK_TIXIAN).initRecordList(withdrawInfos);
                    } else {
                        vp_views.get(XXK_TIXIAN).addRecordInfos(withdrawInfos);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

                break;
        }

        if (isUpdate) {
            vp_views.get(curPage).updateFinish();
            isUpdate = false;
        }
    }

    @Override
    protected void onFailure(int requestCode, String errorCode, String message) {
        if (errorCode.equals("900003")) {    // 查询为空
            int payId;
            try {
                payId = request_type;
            } catch (Exception e) {
                payId = 0;
            }
            List<AccountDetailsBean> infos = new ArrayList<>();
            if(search == 1){
                vp_views.get(curPage).showNoneRecord(message);
            }
            switch (payId) {
                case TYPE_ALL:
                    if (search == 1) {
                        vp_views.get(XXK_ALL).initRecordList(infos);
                    } else {
                        vp_views.get(XXK_ALL).addRecordInfos(infos);
                    }
                    break;
                case TYPE_CZ:
                    if (search == 1) {
                        vp_views.get(XXK_CHONGZHI).initRecordList(infos);
                    } else {
                        vp_views.get(XXK_CHONGZHI).addRecordInfos(infos);
                    }
                    break;
                case TYPE_BET:

                    if (search == 1) {
                        vp_views.get(XXK_BET).initRecordList(infos);
                    } else {
                        vp_views.get(XXK_BET).addRecordInfos(infos);
                    }
                    break;
                case TYPE_PRIZE:
                    if (search == 1) {
                        vp_views.get(XXK_PRIZE).initRecordList(infos);
                    } else {
                        vp_views.get(XXK_PRIZE).addRecordInfos(infos);
                    }
                    break;
                case TYPE_TX:
                    if (search == 1) {
                        vp_views.get(XXK_TIXIAN).initRecordList(infos);
                    } else {
                        vp_views.get(XXK_TIXIAN).addRecordInfos(infos);
                    }
                    break;
                default:
                    break;
            }
        } else {
            vp_views.get(curPage).showFail(message);
        }

    }

    /**
     * 初始化
     */
    private void info() {
        zhhmx_title_rl = (TopTitleView) findViewById(R.id.zhhmx_title_rl);
        zhhmx_title_rl.setCenterText("账户明细");
        zhhmx_title_rl.setRightTxt("全部");
        zhhmx_title_rl.setOnTitleClickListener(this);
       // zhhmx_title_rl.setBackground(R.drawable.bg_account_jianbain);

        balance = (TextView)findViewById(R.id.balance);
        withdraw = (TextView)findViewById(R.id.withdraw);
        freeze = (TextView)findViewById(R.id.freeze);
        rlBg = (RelativeLayout)findViewById(R.id.rl_bg);

        balance.setText(MySharedPreferences.getInstance().getBalance()+"米");
        withdraw.setText(MySharedPreferences.getInstance().getDrawbalance()+"米");
        freeze.setText(MySharedPreferences.getInstance().getFreeze()+"米");

        top_popup = new HM_VerticalPop(this, AbsListView.LayoutParams.WRAP_CONTENT, AbsListView
				.LayoutParams.WRAP_CONTENT,
                popup_items);
        top_popup.setHm_PopupOnItemListener(this);
        zhmx_filter = (RadioGroup) findViewById(R.id.zhmx_filter);
        zhmx_filter.setOnCheckedChangeListener(onCheckedChangeListener);
        bottom_vp = (ViewPager) findViewById(R.id.zhhmx_viewpager);
        for (int i = 0; i < 5; i++) {
            ZhangHuMXRecordView view = new ZhangHuMXRecordView(getContext());
            view.setUpdateInterface(this);
            view.setMoreInterface(this);
            view.setRetryInterface(this);
            vp_views.add(view);
        }
        bottom_vp_adp = new ZhangHuMXPagerAdP(vp_views);
        bottom_vp.setAdapter(bottom_vp_adp);

        bottom_vp.setOnPageChangeListener(new ZhangHuMXPagerChange());

    }

    RadioGroup.OnCheckedChangeListener onCheckedChangeListener = new RadioGroup
			.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId){
                case R.id.zhhmx_all:
                    request_type = TYPE_ALL;
                    search = search_all;
                    bottom_vp.setCurrentItem(XXK_ALL);
                    break;
                case R.id.zhhmx_chongzhi:
                    request_type = TYPE_CZ;
                    search = search_cz;
                    bottom_vp.setCurrentItem(XXK_CHONGZHI);
                    break;
                case R.id.zhhmx_paijiang:
                    request_type = TYPE_PRIZE;
                    search = search_prize;
                    bottom_vp.setCurrentItem(XXK_PRIZE);
                    break;
                case R.id.zhhmx_tixian:
                    request_type = TYPE_TX;
                    search = search_tx;
                    bottom_vp.setCurrentItem(XXK_TIXIAN);
                    break;
//                case R.id.zhhmx_touzhu:
//                    request_type = TYPE_BET;
//                    search = search_bet;
//                    bottom_vp.setCurrentItem(XXK_BET);
//                    break;
            }
        }
    };


    private void requestRecord(int index) {
        // TODO Auto-generated method stub
        switch (index) {
            case XXK_ALL:
                if (search_all == 1) {
                    net();
                }
                break;
            case XXK_CHONGZHI:
                if (search_cz == 1) {
                    net();
                }
                break;
            case XXK_BET:
                if (search_bet == 1) {
                    net();
                }
                break;
            case XXK_PRIZE:
                if (search_prize == 1) {
                    net();
                }
                break;
            case XXK_TIXIAN:
                if (search_tx == 1) {
                    net();
                }
                break;
        }
    }

    private int lastPosition = 0;

    @Override
    public void onItem(int position) {
        if(lastPosition != position){//时间范围发生变化
            search_all = 1;
            search_bet = 1;
            search_cz = 1;
            search_prize = 1;
            search_tx = 1;
        }
        zhhmx_title_rl.setRightTxt(popup_items[position]);
        timeRange = range_time[position];
        search = 1; // pages重置为第一页
        net();
        lastPosition = position;
    }

    @Override
    public void popupShow() {
    }

    @Override
    public void popupDismiss() {
    }

    @Override
    public void onRightClick() {
        top_popup.showPopupWindow(zhhmx_title_rl.getRightTxt());
    }


    /**
     * viewPager 切换子界面事件
     *
     * @author Administrator
     */
    class ZhangHuMXPagerChange implements OnPageChangeListener {
        @Override
        public void onPageScrollStateChanged(int arg0) {
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageSelected(int page) {
            curPage = page;
            zhmx_filter.setOnCheckedChangeListener(null);
            switch (page) {
                case XXK_ALL:
                    zhmx_filter.check(R.id.zhhmx_all);
                    if (search_all == 1) {
                        requestRecord(XXK_ALL);
                    }
                    break;
                case XXK_CHONGZHI:
                    zhmx_filter.check(R.id.zhhmx_chongzhi);
                    if (search_cz == 1) {
                        requestRecord(XXK_CHONGZHI);
                    }
                    break;
                case XXK_BET:
                   // zhmx_filter.check(R.id.zhhmx_touzhu);
                    if (search_bet == 1) {
                        requestRecord(XXK_BET);
                    }
                    break;
                case XXK_PRIZE:
                    zhmx_filter.check(R.id.zhhmx_paijiang);
                    if (search_prize == 1) {
                        requestRecord(XXK_PRIZE);
                    }
                    break;
                case XXK_TIXIAN:
                    zhmx_filter.check(R.id.zhhmx_tixian);
                    if (search_tx == 1) {
                        requestRecord(XXK_TIXIAN);
                    }
                    break;
            }
            zhmx_filter.setOnCheckedChangeListener(onCheckedChangeListener);
        }
    }

    @Override
    public void more() {
        // TODO Auto-generated method stub
        switch (curPage) {
            case XXK_ALL:
                search_all++;
                search = search_all;
                break;
            case XXK_CHONGZHI:
                search_cz++;
                search = search_cz;
                break;
            case XXK_BET:
                search_bet++;
                search = search_bet;
                break;
            case XXK_PRIZE:
                search_prize++;
                search = search_prize;
                break;
            case XXK_TIXIAN:
                search_tx++;
                search = search_tx;
                break;
        }
        net();
    }


    @Override
    public void reTry() {
        requestRecord(curPage);
    }

}
